<?php declare(strict_types=1);


namespace App\Http\Controller;
use App\Rpc\Lib\MessageInterface;
use App\Rpc\Lib\NotifyInterface;
use App\Rpc\Lib\OrderInterface;
use App\Rpc\Lib\PayAccountInterface;
use Exception;
use Ramsey\Uuid\Uuid;
use Six\Rpc\Client\Annotation\Mapping\Compensable;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Six\Rpc\Client\Annotation\Mapping\Reference;
/**
 * Class RpcController
 *
 * @since 2.0
 * @Controller(prefix="/rpc")
 */
class RpcController
{

    /**
     * @\Swoft\Rpc\Client\Annotation\Mapping\Reference(pool="notify.pool")
     *
     * @var NotifyInterface
     */
    private $notifyService;

    /**
     * 在程序初始化的时候定义好服务降级处理类
     * fallback值要与Fallback降级类中的name要一一对应
     * @Reference(pool="order.pool" , fallback="OrderFallback" , type="tcc")
     *
     * @var OrderInterface
     */
    private $orderService;

    /*
     * 在程序初始化的时候定义好服务降级处理类
     * fallback值要与Fallback降级类中的name要一一对应
     * @Reference(pool="order.pool" , fallback="OrderFallback")
     *
     * @var OrderInterface
     */
    private $orderService1;

    /**
     *
     * @Reference(pool="payAccount.pool" , fallback="PayAccountFallback" ,type="tcc")
     *
     * @var PayAccountInterface
     */
    private $payAccount;


    /**
     * 在程序初始化的时候定义好服务降级处理类
     * @Reference(pool="message.pool" , fallback="MessageFallback")
     *
     * @var MessageInterface
     */
    private $messageService;


    /**
     * @RequestMapping("order")
     * @return array
     */
    public function order()
    {

        $msg_id = Uuid::uuid1();
//        $prepareMsgData = [
//            'msg_id'        => $msg_id,
//            'version'       => 1, #版本号
//            'create_time'   => time(), #创建时间
//            'message_body'  => [
//                'order_id'  => 112,
//                'shop_id'   => 2
//            ],
//            'consumer_queue' => 'order',   #消费队列
//            'message_retries_number' => 0 ,#重试次数
//            'status' => 1 ,#消息状态（1：预发送，2：发送中:3：已消费:4：已死亡）
//        ];
        $msg_body=[
            'msg_id'=>uniqid(),
            'version'=>1,
            'create_time'=>time(),
            'message_body'=>['order_id'=>12133,'shop_id'=>2],
            'notify_url'=>'http://127.0.0.1:9805/notify/index',
            'notify'=>[1=>5,2=>10,3=>15],//单位为秒
            'notify_retries_number'=>0, //重试次数，
            'default_delay_time'=>1,//毫秒为单位
            'status'=>1, //消息状态
        ];
        var_dump($this->notifyService->publish($msg_body));
//        //消息通知
        return ['notify'];
        $data = [
          'msg_id' => $msg_id,
        ];
        //预发送消息
        $result = $this->messageService->prepareMsg($msg_body);
//        var_dump($result);
        if($result['status'] == 1){ //预存储消息
            if($this->orderService->update($data)['status'] == 1){ //
                $res=$this->messageService->confirmMsgToSend($msg_id,1);
                var_dump($res);
                if($res['status'] == 1){
                    return [$result];
                }
            }

        }

        return [$result];
    }


    /**
     * @RequestMapping("success")
     * @\Six\TCC\Annotation\Mapping\Compensable(services=OrderInterface::class,{"tryMethod"="creditOrderTcc","confirmMethod"="confirmCreditOrderTcc","cancelMethod"="cancelCreditOrderTcc"})
     * @\Six\TCC\Annotation\Mapping\Compensable(services=PayAccountInterface::class,assoc=OrderInterface::class,{"tryMethod"="creditAccountTcc","confirmMethod"="confirmCreditAccountTcc","cancelMethod"="cancelCreditAccountTcc"})
     * @return array
     */
    public function orderSuccess()
    {

//        var_dump(111);
        $context=[];
        //主服务触发TCC型调用，从服务只需要定义不需要触发
        $this->orderService->creditOrderTcc($context);
//        $this->payAccount->creditAccountTcc($context);
        return ['try-success'];
    }

    /**
     * @RequestMapping("returnBool")
     *
     * @return array
     */
    public function returnBool(): array
    {
        $result = $this->userService->delete(12);

        if (is_bool($result)) {
            return ['bool'];
        }

        return ['notBool'];
    }

    /**
     * @RequestMapping()
     *
     * @return array
     */
    public function bigString(): array
    {
        $this->userService->getBigContent();

        return ['string'];
    }

    /**
     * @RequestMapping()
     *
     * @return array
     *
     * @throws Exception
     */
    public function exception(): array
    {
        $this->userService->exception();

        return ['exception'];
    }
}